// import { Toast } from "vant";
import { Collapse, CollapseItem } from 'vant';
const documentElement = document.documentElement;
const body = document.body;
window.page_idAdvertisings = []; // 浏览周期内只显示一次
window.page_idFull_screens = [];

export default {
  data() {
    return {
      clientWidth: "375",
      showPage: true,
      isBottom: false,
      btnFlag: false,
      isMounted: false,
      // current_page: 1,
      total_page: 0,
      isLoadMore: true,

      categoryList: [],
      active: 0,
      flxShow: 1,
      activeNames: ['1'],
      secondaryId: 0,
      page: 1,
      foodInfo: [],
      LoadMoreTest: "没有更多",
      isBottomReached: false, // 是否触底的标志  
    };
  },
  mounted() {
    // window.diyHomeScrollTop = 0;
    // this.isInHome = 1;
    // this.getTemp();

    // // mounted 肯定是第一次进来
    // this.isMounted = true;
  },
  activated() {
    this.getCategoryList()
    // this.isInitVideoScrollFn = true;
    // if (this.fun.getPhoneEnv() == 3) {
    //   this.clientWidth = 375;
    // } else {
    //   this.clientWidth = document.body.clientWidth;
    // }

    // if (window.page_id != this.fun.getKey("page_id") || window.isInHome) {
    //   if (!this.isMounted) {
    //     // mounted 请求了这里就不请求
    //     this.fun.setWXTitle("");
    //     this.isInHome = 1;
    //     this.getTemp();
    //   }
    // } else {
    //   let title = window.localStorage.getItem("diypage_title");
    //   this.fun.setWXTitle(title);
    //   if (window.location.href.indexOf("#/newDiy?") > -1) {
    //     setTimeout(() => {
    //       window.scrollTo(0, window.diyHomeScrollTop);
    //     }, 200);
    //   }
    // }

    // if (this.isMounted) {
    //   // 防止下次进入同样的页面还请求
    //   this.isMounted = false;
    // }
    // this.page_id = this.fun.getKey("page_id");
    // window.page_id = this.fun.getKey("page_id");
    // window.isInDiy = 1; // 判断是否进入了装修页
    // window.addEventListener("scroll", this.debounce(this.handleScroll, 50));
  },
  deactivated() {
    window.removeEventListener("scroll", this.debounce(this.handleScroll, 50));
  },
  beforeRouteLeave(to, from, next) {
    window.diyHomeScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    next();
  },
  methods: {
    gotosearch() {

    },
    gotoGoods(){},
    chooseSecondary(id) {
      this.secondaryId = id
      this.page = 1
      this.foodInfo = []
      this.getSecGoodsList()
    },
    tabsa(index) {
      this.active = index
      this.recommend = this.categoryList[index].child
      this.secondaryId = this.categoryList[index].child[0].id
      this.page = 1
      this.foodInfo = []
      this.getSecGoodsList()
    },
    getSecGoodsList(){
      $http
        .get(
          "HuanNiu.get_store_list",
          {
            cate_id: this.secondaryId,
            page: this.page
          },
          ".."
        )
        .then(response => {
          if (response.result == 1) {
            console.log(response.data,'llllll')
            let newDate = response.data.data
            this.foodInfo = [...this.foodInfo,...newDate] 
            this.total_page = response.data.last_page
            
            // this.recommend = this.categoryList[0].child
            // this.secondaryId = this.categoryList[0].child[0].id

            //   this.isLoadMore = true;
            //   this.components = this.components.concat(response.data.datas);
          } else {
            this.isLoadMore = false;
            this.current_page = this.current_page - 1;
          }
        });
    },
    getCategoryList() {
      $http
        .get(
          "HuanNiu.get_store_cate",
          {

          },
          ".."
        )
        .then(response => {
          if (response.result == 1) {
            console.log(response.data)
            this.categoryList = response.data
            this.recommend = this.categoryList[0].child
            this.secondaryId = this.categoryList[0].child[0].id
            this.getSecGoodsList()
            //   this.isLoadMore = true;
            //   this.components = this.components.concat(response.data.datas);
          } else {
            this.isLoadMore = false;
            this.current_page = this.current_page - 1;
          }
        });
    },
    getMoreDecorate() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.current_page < this.total_page) {
        this.current_page = this.current_page + 1;
        $http
          .get(
            "plugin.decorate.frotend.decorate-api.get-page",
            {
              decorate_page: this.current_page,
              decorate_id: this.page_id
            },
            ".."
          )
          .then(response => {
            if (response.result == 1) {
              this.isLoadMore = true;
              this.components = this.components.concat(response.data.datas);
            } else {
              this.isLoadMore = false;
              this.current_page = this.current_page - 1;
            }
          });
      } else {
        console.log("加载完了");
      }
    },
    //获取滚动条当前的位置
    getScrollTop() {
      var scrollTop = 0;
      if (documentElement && documentElement.scrollTop) {
        scrollTop = documentElement.scrollTop;
      } else if (body) {
        scrollTop = body.scrollTop;
      }
      return scrollTop;
    },
    //获取当前可视范围的高度
    getClientHeight() {
      var clientHeight = 0;
      if (body.clientHeight && documentElement.clientHeight) {
        clientHeight = Math.min(body.clientHeight, documentElement.clientHeight);
      } else {
        clientHeight = Math.max(body.clientHeight, documentElement.clientHeight);
      }
      return clientHeight;
    },
    //获取文档完整的高度
    getScrollHeight() {
      return Math.max(body.scrollHeight, documentElement.scrollHeight);
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function (args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function () {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    },
    // handleScroll(event) {
    //   console.log(event,'event')
    //   const container = event.target;  
    //   const content = container.querySelector('.goodsList');  
    //   const contentHeight = content.scrollHeight;  
    //   const scrollTop = container.scrollTop;  
    //   const clientHeight = container.clientHeight;  
  
    //   // 当滚动到底部时触发  
    //   if (scrollTop + clientHeight >= contentHeight) {  
    //     this.isBottomReached = true;  
  
    //     // 这里可以添加加载更多数据的逻辑  
    //     // 例如，你可以调用一个API来获取更多数据，并在数据加载完成后设置isBottomReached为false  
  
    //     // 示例：模拟数据加载  
    //     setTimeout(() => {  
    //       this.isBottomReached = false;  
    //       // 在这里你可能需要更新items数组以显示新加载的数据  
    //     }, 2000);  
    //   } else {  
    //     this.isBottomReached = false;  
    //   }  





    //   // let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    //   // if (scrollTop > 60) {
    //   //   this.btnFlag = true;
    //   // } else {
    //   //   this.btnFlag = false;
    //   // }

    //   // //滚动事件触发
    //   // if (Number(this.getScrollTop() + this.getClientHeight() + 105) >= this.getScrollHeight()) {
    //   //   if (this.isLoadMore) {
    //   //     this.getMoreDecorate();
    //   //   }
    //   //   //此处发起请求
    //   //   this.isBottom = true;
    //   // } else {
    //   //   this.isBottom = false;
    //   // }

    //   // this.videoScrollHandle(); //监听处理视频组件逻辑

    // },
    handleScroll(event) {  
      // console.log(event,'event')
      const container = event.target;  
      // console.log(container,'container')
      const content = container.querySelector('.goodsList');  
      // console.log(content,'kkkkkkkk')
      const contentHeight = content.scrollHeight;  
      const scrollTop = container.scrollTop;  
      const clientHeight = container.clientHeight;  
  
      // 当滚动到底部时触发  
      if (scrollTop + clientHeight >= contentHeight) {  
        this.isBottomReached = true;
        if(this.total_page <= this.page){
          return
        }
        this.page += 1;
        this.getSecGoodsList()
  
        // 这里可以添加加载更多数据的逻辑  
        // 例如，你可以调用一个API来获取更多数据，并在数据加载完成后设置isBottomReached为false  
  
        // 示例：模拟数据加载  
        setTimeout(() => {  
          this.isBottomReached = false;
          // 在这里你可能需要更新items数组以显示新加载的数据  
        }, 2000);  
      } else {  
        this.isBottomReached = false;  
      }  
    }, 

  },
  components: {

  }
};
